<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
  import echarts from 'echarts'

  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        const labelTop = {
          normal: {
            label: {
              show: true,
              position: 'center',
              formatter: '{b}',
              textStyle: {
                baseline: 'bottom'
              }
            },
            labelLine: {
              show: false
            }
          }
        }
        const labelFromatter = {
          normal: {
            label: {
              formatter: function(params) {
                return 100 - params.value + '%'
              },
              textStyle: {
                baseline: 'top'
              }
            }
          }
        }
        var labelBottom = {
          normal: {
            color: '#ccc',
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            }
          },
          emphasis: {
            color: 'rgba(0,0,0,0)'
          }
        };
        const radius = [40, 55]
        this.chart = echarts.init(this.$el, 'macarons')

        this.chart.setOption({
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            x: 'center',
            y: 'center',
            data: [
              'GoogleMaps', 'Facebook', 'Youtube', 'Google+', 'Weixin',
              'Twitter', 'Skype', 'Messenger', 'Whatsapp', 'Instagram'
            ]
          },
          title: {
            text: 'The App World',
            subtext: 'from global web index',
            x: 'center'
          },
          toolbox: {
            show: true,
            feature: {
              dataView: { show: true, readOnly: false },
              magicType: {
                show: true,
                type: ['pie', 'funnel'],
                option: {
                  funnel: {
                    width: '20%',
                    height: '30%',
                    itemStyle: {
                      normal: {
                        label: {
                          formatter: function(params) {
                            return 'other\n' + params.value + '%\n'
                          },
                          textStyle: {
                            baseline: 'middle'
                          }
                        }
                      }
                    }
                  }
                }
              },
              restore: { show: true },
              saveAsImage: { show: true }
            }
          },
          series: [
            {
              type: 'pie',
              center: ['10%', '30%'],
              radius: radius,
              x: '0%', // for funnel
              itemStyle: labelFromatter,
              data: [
                { name: 'other', value: 46, itemStyle: labelBottom },
                { name: 'GoogleMaps', value: 54, itemStyle: labelTop }
              ]
            }]
        })
      }
    }
  }
</script>
